﻿using System.Collections.Generic;
using System.Linq;
using GTAuto.DAL.Entities;


namespace GTAuto.DAL.DAL
{
    public class DALKladr : DALBase
    {
        public static DALKladr Singleton = new DALKladr();

        public GTRegion RegionAdd(GTRegion gtRegion)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newDic = new Region();
                newDic.Id = gtRegion.Id;
                newDic.KladrCode = gtRegion.KladrCode;
                newDic.Name = gtRegion.Name;
                newDic.Type = gtRegion.Type;

                db.Regions.InsertOnSubmit(newDic);
                db.SubmitChanges();

                return gtRegion;
            }
        }

        public List<GTRegion> RegionsGetPaged()
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Regions
                 orderby p.Name
                 select p;

                var brands = brds.ToList();
                var list = new List<GTRegion>();
                foreach (var region in brands)
                {
                    var temp = new GTRegion();
                    temp.UpdateTo(region);
                    list.Add(temp);
                }


                return list;
            }
        }
        
        
        public void RegionDelete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var singleOrDefault = db.Regions.SingleOrDefault(x => x.Id == id);
                if (singleOrDefault != null)
                {
                    db.Regions.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }

        public GTCity CityAdd(GTCity gtCity)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newDic = new City();
                //newDic.Id = gtCity.Id;
                newDic.KladrCode = gtCity.KladrCode;
                newDic.Name = gtCity.Name;
                newDic.RregionId = gtCity.RregionId;

                db.Cities.InsertOnSubmit(newDic);
                db.SubmitChanges();

                return gtCity;
            }
        }

        public List<GTCity> CitiesGetPaged(int regionId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Cities
                 where p.RregionId == regionId
                 orderby p.Name
                 select p;

                var brands = brds.ToList();
                var list = new List<GTCity>();
                foreach (var region in brands)
                {
                    var temp = new GTCity();
                    temp.UpdateTo(region);
                    list.Add(temp);
                }


                return list;
            }
        }


        public void CityDelete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var singleOrDefault = db.Cities.SingleOrDefault(x => x.Id == id);
                if (singleOrDefault != null)
                {
                    db.Cities.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }

       


    }
}
